package com.iapcursos.portal.procefet.controle;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

import com.iapcursos.portal.procefet.utils.FileUtils;
import com.iapcursos.portal.procefet.utils.FileWEB;

public class MediaPageBean {
	private FileUtils fileUtils;
	private String originalDir;
	private String dir;
	private List<FileWEB> files;
	private int uploadsAvailable = 50;
	private List<String> diretorios;
	private String pasta;

	public String getPasta() {
		return pasta;
	}

	public void setPasta(String pasta) {
		this.pasta = pasta;
	}

	@SuppressWarnings("unchecked")
	public MediaPageBean(){
		fileUtils = new FileUtils();
		files = new ArrayList<FileWEB>();
		diretorios = new ArrayList<String>();
		dir = getDirApplication();
		originalDir = getDirApplication();
	}
	
	public String getDirApplication(){
		FacesContext context = FacesContext.getCurrentInstance();
		ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
		String caminho = servletContext.getRealPath("/" );
		
		caminho += "admin" + File.separator + "media" + File.separator;
		this.pasta = "media/";
		return caminho;
	}
	
	public List<String> getDiretorios() {
		this.diretorios = fileUtils.getDirectories(this.dir);		
		return diretorios;
	}

	public void setDiretorios(List<String> diretorios) {
		this.diretorios = diretorios;
	}

	public int getUploadsAvailable() {
		return uploadsAvailable;
	}

	public void setUploadsAvailable(int uploadsAvailable) {
		this.uploadsAvailable = uploadsAvailable;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public List<FileWEB> getFiles() {
		this.files.clear();
		
		File[] filesArray = fileUtils.getFilesByDirectory(this.dir);
		
		for (File file : filesArray) {
			this.files.add(new FileWEB(file.getName(), file.length(), file.lastModified()));
		}
		
		return files;
	}

	public void setFiles(List<FileWEB> files) {
		this.files = files;
	}
	
	public String selecionarPasta(){
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> map = context.getExternalContext().getRequestParameterMap();
		String diretorio = map.get("pasta");
		
		if(!diretorio.equals("media")){
			this.dir = originalDir + diretorio + File.separator;
			this.pasta = "media/" + diretorio + "/";
		}else{
			this.dir = originalDir;
			this.pasta = "media/";
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public void listener(UploadEvent event){
		UploadItem item = event.getUploadItem();
		File file = item.getFile();
		
		FileUtils.copyFile(file, item.getFileName(), this.dir);
	}
	
	public String excluirArquivo(){
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> map = context.getExternalContext().getRequestParameterMap();
		String nomeArquivo = map.get("arquivo");
		File arquivo = new File(this.getDirApplication()+File.separator+nomeArquivo);
		System.out.println("opa entrou aqui...");
		arquivo.delete();
		return null;
	}
}
